package net.comm.util;

import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.IOException;   
import java.util.ArrayList;   
import java.util.Iterator;   
import java.util.List;   
  
import org.jdom.Document;   
import org.jdom.Element;   
import org.jdom.JDOMException;   
import org.jdom.input.SAXBuilder;   
  
public class ConfigXml {   
       
    public static void main(String[] args) {   
        read("config.xml");   
    }   
       
    public static List<BaseConnBean> read(){   
        return read("config.xml");   
    }   
  
    public static List<BaseConnBean> read(String filename) { 
    	String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();  
        String rpath = path + filename;   
//        String rpath = ConfigXml.class.getResource("").getPath().substring(1)   
//                + path;   
        FileInputStream fi = null;   
        List<BaseConnBean> pools=new ArrayList<BaseConnBean>();   
        try {   
            fi = new FileInputStream(rpath);   
            SAXBuilder sb = new SAXBuilder();   
            Document doc = sb.build(fi);   
            Element root = doc.getRootElement();   
            List list=root.getChildren();   
            Element pool = null;   
            Iterator allPool = list.iterator();   
            while(allPool.hasNext()){   
                pool=(Element) allPool.next();   
                BaseConnBean bcBean=new BaseConnBean();   
                bcBean.setName(pool.getChildText("name"));   
                   
                bcBean.setUsername(pool.getChildText("username"));   
                bcBean.setPassword(pool.getChildText("password"));   
                bcBean.setJdbcurl(pool.getChildText("jdbcurl"));   
                try{   
                bcBean.setMax(Integer.parseInt(pool.getChildText("max")));   
                }catch(NumberFormatException e){   
                    bcBean.setMax(0);   
                }   
                try{   
                bcBean.setWait(Long.parseLong(pool.getChildText("wait")));   
                }catch(NumberFormatException e){   
                    bcBean.setWait(-1L);   
                }   
                bcBean.setDriver(pool.getChildText("driver"));   
                pools.add(bcBean);   
            }   
        } catch (FileNotFoundException e) {   
            System.out.println("file does not find");   
            e.printStackTrace();   
        } catch (JDOMException e) {   
            System.out.println("jdom exception");   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        return pools;   
    }   
} 
